
clear all 

//define folder global here

gl Inputs "$folder\1_Inputs"
gl Intermediate "$folder\2_Intermediate"
gl Outputs "$folder\3_Outputs"


********************************************************************************
**# TABLE 2: FIRST STAGE
********************************************************************************

****PREPARE NY DATA

use "${Inputs}\NY_main_fs.dta", clear 

	rename (beta se N) (numbeta numse numN)
	reshape long num, i(specification coef_var) j(v2) string

	gen 	controls = "No Controls" if specification=="First Stage, No Controls"
	replace controls = "Controls" if specification=="First Stage, with Controls"
	
	gen 	value = "obs" if v2=="N"

	replace value = "js_co" 	if regexm(coef, "Instrument") & v2=="beta"
	replace value = "js_se" 	if regexm(coef, "Instrument") & v2=="se"
	
	replace value = "alt_js_co" if regexm(coef, "Stays Stringency") & v2=="beta"
	replace value = "alt_js_se"	if regexm(coef, "Stays Stringency") & v2=="se"
	
	replace value = "ea_co" if regexm(coef, "Emergency") & v2=="beta"
	replace value = "ea_se" if regexm(coef, "Emergency") & v2=="se"
	
	gen 	spec = "5" if specification=="First Stage, No Controls"
	replace spec = "6" if specification=="First Stage, with Controls"
	replace spec = "7" if specification=="First Stage, with Other Stringency"
	
	drop specification coef_var v2
	
	gen geo = "ny"
	
	tempfile ny_data
	save `ny_data'

****PREPARE COOK COUNTY DATA

import delimited "${Inputs}\Cook_lehd_fs.csv", clear
	
	keep if pop=="all=1"
	drop pop 
	
	rename *loo_vc_ivsamp100_* *js_*
	rename trim_amnt* alt*
	rename * num* 
	rename (numspecification numcontrols) (spec controls)
	
	reshape long num, i(spec controls) j(value) string
	drop if mi(num)
	
	replace value = subinstr(value, "_coeff", "_co", .)
	replace value = "obs" if value=="observations"
	replace spec = subinstr(spec, "(", "", .)
	replace spec = subinstr(spec, ")", "", .)
	
	gen geo = "chi"
	append using `ny_data'
	
	gen num2 = string(num, "%12.3fc")
	replace num2 = subinstr(num2, ".000", "", .)
	drop num 
	duplicates drop 
	
	** add stars and parentheses 
	replace num2 = subinstr(num2, ",", "", .)
	destring num2, replace 
	reshape wide num2, i(spec controls geo) j(value) string
	rename num2* num_*
	
	foreach i in alt_js ea js {
		gen pval_`i' = 2*(1-normal(abs(num_`i'_co / num_`i'_se)))
		gen 	stars_`i' = "***" 	if pval_`i' < 0.01
		replace stars_`i' = "**" 	if pval_`i' < 0.05 	& mi(stars_`i')
		replace stars_`i' = "*" 	if pval_`i' < 0.1 	& mi(stars_`i')	
		
		gen num_`i'_co2 = string(num_`i'_co, "%10.3fc") + stars_`i'
		gen num_`i'_se2 = "(" + string(num_`i'_se, "%10.3fc") + ")"
		
		drop num_`i'_co num_`i'_se pval_`i' stars_`i'
	}
	
	rename (*_co2 *_se2) (*_co *_se)
	
	tostring num_f_amnt num_f_both num_f_inst, replace
	gen num_obs2 = string(num_obs, "%10.0fc")
	drop num_obs
	rename num_obs2 num_obs
	
	reshape long num_ , i(spec controls) j(value) string
	drop if num_ == "(.)" | num_=="."
	
	** add controls 
	preserve 
		contract spec controls 
		drop _freq 
		rename controls num_ 
		gen value = "cntrls"
		
		tempfile controls_fortable
		save `controls_fortable'
	restore 
	
	drop controls 
	append using `controls_fortable'
	
	isid spec value

	levelsof spec, local(specs)
	levelsof value, local(valus)

foreach spex in `specs' {
	local id1 = "`spex'"
	
	foreach valx in `valus' {
		local id2 = "`valx'"
		
		levelsof num if spec=="`spex'" & value=="`valx'", ///
		clean local(v_`id1'_`id2') 
		
		di "v_`id1'_`id2'"		
	}
}
				

texdoc init "${Outputs}/Table_2.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c c c c} \hline \toprule  ///
& \multicolumn{2}{c}{\textbf{Cook County}} & & \multicolumn{2}{c}{\textbf{New York}} \\
tex  & (1) & (2) & & (3) & (4) \\
tex \cline{2-3} \cline{5-6}
tex & & & & & \\
tex Judge stringency 		& `v_1_js_co' 		& `v_2_js_co'		& & `v_5_js_co' 		& `v_6_js_co' \\
tex 							& `v_1_js_se' 		& `v_2_js_se' 		& & `v_5_js_se' 		& `v_6_js_se' \\

tex Controls 				& No 				& Yes 				& & No 					& Yes \\

tex \midrule	
tex Observations				& `v_1_obs' 		& `v_2_obs' 		& & `v_5_obs' 			& `v_6_obs' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close



********************************************************************************
********************************************************************************
**# TABLE 3: BALANCE TABLE
********************************************************************************

****PREPARE NY DATA
use "${Inputs}\NY_main_balance.dta", clear 

	drop *_cc_nyc

	foreach i in evict judge {

		gen		zscore_`i' = beta_`i' / se_`i' 
		gen 	pvalue_`i' = 2*(1-normal(abs(zscore_`i')))
		
		gen 	stars_`i' = "***" if pvalue_`i' < 0.01
		replace stars_`i' = "**" if pvalue_`i' < 0.05 & mi(stars_`i')
		replace stars_`i' = "*" if pvalue_`i' < 0.1 & mi(stars_`i')	
		
		gen 	beta_`i'_2 = string(beta_`i', "%10.5fc")
		replace beta_`i'_2 = beta_`i'_2 + stars_`i'
		replace beta_`i'_2 = "" if beta_`i'_2=="."
		
		gen 	se_`i'_2 = string(se_`i', "%10.5fc")
		replace se_`i'_2 = "(" + se_`i'_2 + ")"
		replace se_`i'_2 = "" if se_`i'_2=="(.)"
		
		gen N_`i'_2 = string(N_`i', "%12.0fc")
		gen F_`i'_2 = string(F_`i', "%10.4fc")
		gen pval_`i'_2 = string(pval_`i', "%10.4fc")
	}
	
	keep variablename *_2
	rename *_2 num*

	reshape long num , i(variablename) j(value) string
	split value, parse("_")
	drop value 
	rename (value1 value2) (value reg)
	drop if mi(num)
	
	replace reg = "ev" if reg=="evict"
	replace reg = "js" if reg=="judge"
	
	replace value = "co" if value=="beta"
	replace value = "obs" if value=="N"
	replace value = "jfs" if value=="F"
	replace value = "jpv" if value=="pval"
	
	rename variablename variable 
	replace variable = "age" if variable=="Age at Filing"
	replace variable = "age2" if variable=="Age^2"
	replace variable = "age3" if variable=="Age^3"
	replace variable = "fem" if variable=="Female"
	replace variable = "cash" if variable=="Cash Assistance, Past Year"
	replace variable = "emer" if variable=="Emergency Assistance, Past Year"
	replace variable = "hisp" if variable=="Hispanic"
	replace variable = "earn" if variable=="Quarterly Earnings"
	replace variable = "empl" if variable=="Quarterly Employment"
	replace variable = "rent" if variable=="Tract Median Rent"
	replace variable = "stab" if variable=="Rent Stabilized"
	replace variable = "povr" if variable=="Tract Poverty Rate"
	replace variable = "prio" if variable=="First Case"
	replace variable = "att" if variable=="No Attorney"
	replace variable = "whi" if variable=="White, Not-Hispanic"
	replace variable = "dam" if variable=="Rent Amount Owed"
	replace variable = "dam2" if variable=="Rent Amount Owed^2"
	replace variable = "bla" if variable=="Black"

	replace variable = "x" if !inlist(value, "co", "se")
	
	gen geo = "ny"
	
	replace num = "224.8" if value=="jfs" & reg=="ev"
	replace num = "1.007" if value=="jfs" & reg=="js"
	replace num = "0.000" if value=="jpv" & reg=="ev"
	replace num = "0.443" if value=="jpv" & reg=="js"

	
	tempfile ny_data
	save `ny_data'


****PREPARE COOK COUNTY DATA

import delimited "${Inputs}\Cook_lehd_balance.csv", clear

	rename (v1 v2 v3) (name Evicted JudgeStrin)
	
	gen 	value = "co" if !mi(name)
	replace value = "se" if mi(name)
	replace value = "obs" if name=="Number of observations"
	replace value = "jfs" if name=="Joint F-Test Stat."
	replace value = "jpv" if name=="p-value"
	replace value = "fs" if name=="F incld miss ind"
	replace value = "pv" if name=="p-val incld miss ind"
	
	replace name = name[_n-1] if mi(name)
	drop if inlist(name, "Title: IV Balance Table", "A")

	gen 	variable = "age" if name=="ageatcase"
	replace variable = "age2" if name=="ageatcase_sq"
	replace variable = "age3" if name=="ageatcase_cub"
	replace variable = "fem" if name=="fem"
	replace variable = "femb" if name=="fem_black"
	replace variable = "femh" if name=="fem_hisp"
	replace variable = "femw" if name=="fem_white"
	replace variable = "hisp" if name=="hispanic"
	replace variable = "joi" if name=="joint"
	replace variable = "povr" if name=="nbrhd_pov_rate_5yr"
	replace variable = "povr2" if name=="nbrhd_pov_rate_5yr_sq"
	replace variable = "prio" if name=="noprior"
	replace variable = "whi" if name=="white"
	replace variable = "dam" if name=="ad_damnum"
	replace variable = "bla" if name=="black"
	
	replace variable = "x" if !inlist(value, "co", "se")
	drop name 
	
	rename (Evicted JudgeStrin) (numev numjs)
	
	reshape long num , i(variable value) j(reg) string
	replace num = "301,000" if num=="301000" & value=="obs"
	replace num = "268,000" if num=="268000" & value=="obs"
	
	gen geo = "chi"

	
append using `ny_data'

order reg variable value geo num 
sort reg variable value geo 

duplicates drop 
isid reg variable value geo 

levelsof reg, local(regs)
levelsof value, local(vals)
levelsof variable, local(vars)
levelsof geo, local(geos)

foreach regx in `regs' {
	local id1 = "`regx'"
	
	foreach varx in `vars' {
		local id2 = "`varx'"
	
		foreach valx in `vals' {
			local id3 = "`valx'"
			
			foreach geox in `geos' {
				local id4 = "`geox'"
				
				levelsof num if reg=="`regx'" & ///
								value=="`valx'" & ///
								variable=="`varx'" & ///
								geo=="`geox'", ///
								clean local(`id1'_`id2'_`id3'_`id4')
				
				di "`id1'_`id2'_`id3'_`id4'"
				
}
}
}
}


texdoc init "${Outputs}/Table_3.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c c c c} \hline \toprule  ///
& \multicolumn{2}{c}{\textbf{Cook County}} & & \multicolumn{2}{c}{\textbf{New York}} \\
tex \cline{2-3} \cline{5-6}
tex $x & Evicted & Stringency & & Evicted & Stringency \\
tex  & (1) & (2) & & (3) & (4) \\
tex \cline{2-3} \cline{5-6}
tex & & & & & \\
tex Age at case 	& `ev_age_co_chi' & `js_age_co_chi' & & `ev_age_co_ny' & `js_age_co_ny' \\	
tex  			& `ev_age_se_chi' & `js_age_se_chi' & & `ev_age_se_ny' & `js_age_se_ny' \\

tex  Female 	& `ev_fem_co_chi' & `js_fem_co_chi' & & `ev_fem_co_ny' & `js_fem_co_ny' \\	
tex  			& `ev_fem_se_chi' & `js_fem_se_chi' & & `ev_fem_se_ny' & `js_fem_se_ny' \\

tex  Black 		& `ev_bla_co_chi' & `js_bla_co_chi' & & `ev_bla_co_ny' & `js_bla_co_ny' \\	
tex  			& `ev_bla_se_chi' & `js_bla_se_chi' & & `ev_bla_se_ny' & `js_bla_se_ny' \\

tex White 		& `ev_whi_co_chi' & `js_whi_co_chi' & & `ev_whi_co_ny' & `js_whi_co_ny' \\	
tex  			& `ev_whi_se_chi' & `js_whi_se_chi' & & `ev_whi_se_ny' & `js_whi_se_ny' \\

tex  Hispanic 	& `ev_hisp_co_chi' & `js_hisp_co_chi' & & `ev_hisp_co_ny' & `js_hisp_co_ny' \\	
tex  			& `ev_hisp_se_chi' & `js_hisp_se_chi' & & `ev_hisp_se_ny' & `js_hisp_se_ny' \\

tex Neighborhood poverty rate (5 yr avg) & `ev_povr_co_chi' & `js_povr_co_chi' & & `ev_povr_co_ny' & `js_povr_co_ny' \\	
tex  			& `ev_povr_se_chi' & `js_povr_se_chi' & & `ev_povr_se_ny' & `js_povr_se_ny' \\

tex Ad damnum (in 1000s) & `ev_dam_co_chi' & `js_dam_co_chi' & & `ev_dam_co_ny' & `js_dam_co_ny' \\	
tex  			& `ev_dam_se_chi' & `js_dam_se_chi' & & `ev_dam_se_ny' & `js_dam_se_ny' \\

tex No prior case & `ev_prio_co_chi' & `js_prio_co_chi' & & `ev_prio_co_ny' & `js_prio_co_ny' \\	
tex  			& `ev_prio_se_chi' & `js_prio_se_chi' & & `ev_prio_se_ny' & `js_prio_se_ny' \\

tex Joint action & `ev_joi_co_chi' & `js_joi_co_chi' & & `ev_joi_co_ny' & `js_joi_co_ny' \\	
tex  			& `ev_joi_se_chi' & `js_joi_se_chi' & & `ev_joi_se_ny' & `js_joi_se_ny' \\

tex \midrule
tex Observations & `ev_x_obs_chi' & `js_x_obs_chi' & & `ev_x_obs_ny' & `js_x_obs_ny' \\	
tex \midrule

tex Joint F-Statistic & `ev_x_jfs_chi' & `js_x_jfs_chi' & & `ev_x_jfs_ny' & `js_x_jfs_ny' \\	

tex P-Value & `ev_x_jpv_chi' & `js_x_jpv_chi' & & `ev_x_jpv_ny' & `js_x_jpv_ny' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close

